package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.common.Constant;
import com.example.demo.model.entity.AreaModel;
import com.example.demo.service.impl.AreaService;
import com.example.demo.util.excel.GeneralExcelPoi;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping(value = Constant.API_VERSION + "/area")
public class AreaController {

    @Autowired
    AreaService areaService;

    @RequestMapping(value = "", method = RequestMethod.GET)
    @ResponseBody
    public JSONObject find(@RequestParam(defaultValue = "26.08") String lat, @RequestParam(defaultValue = "119.28") String lng) throws Exception {
        RestTemplate restTemplate = new RestTemplate();
//        String resp = restTemplate.getForObject("http://api.map.baidu.com/geocoder/v2/?location=38.76623,116.43213&output=json&pois=1&ak=dX0qMG3cGh0VcFmxekhYPwvKtPwzYXnd", String .class);
        String resp = restTemplate.getForObject("http://api.map.baidu.com/geocoder/v2/?location=" + lat + "," + lng + "&output=json&pois=1&ak=dX0qMG3cGh0VcFmxekhYPwvKtPwzYXnd", String.class);
        JSONObject json = JSON.parseObject(resp);
        JSONObject result = new JSONObject();
        if (StringUtils.isEmpty(json.getJSONObject("result").getString("formatted_address"))) {
            throw new RuntimeException(MessageFormat.format(Constant.ERROR_FORMAT, 400, "未匹配的经纬度"));
        }
        result.put("city", json.getJSONObject("result").getJSONObject("addressComponent").getString("city"));
        String areaCode = json.getJSONObject("result").getJSONObject("addressComponent").getString("adcode");
        AreaModel area = areaService.findByAreaCode(areaCode);
        result.put("city_code", area.getCityCode());
        return result;

    }

//    @RequestMapping(value = "/import", method = RequestMethod.POST)
//    @ResponseBody
//    public void upload(MultipartFile file) throws Exception {
//        GeneralExcelPoi poi = new GeneralExcelPoi();
//        Map<String, String> map = new HashMap<>();
//        map.put("B", "provinceName");
//        map.put("C", "cityName");
//        map.put("D", "areaName");
//        map.put("E", "provinceCode");
//        map.put("F", "cityCode");
//        map.put("G", "areaCode");
//        List<AreaModel> list = poi.parseExcel(file.getInputStream(), AreaModel.class, map, 1);
//        for (AreaModel a : list) {
//            a.setAreaId(UUID.randomUUID().toString());
//            areaService.save(a);
//        }
//
//    }


}
